package cn.edu.zafu.bigdata.weier;

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("192.168.16.69", 1234);
            System.out.println("Connected to the server.");

            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(System.in));

            String serverMessage;
            String clientMessage;

            while (true) {
                System.out.print("Enter message: ");
                clientMessage = stdInput.readLine();

                // Send message to server
                output.println(clientMessage);

                // Read server's response
                serverMessage = input.readLine();
                System.out.println("Server says: " + serverMessage);

                // Optional: Close the connection if a specific message is received
                if ("bye".equalsIgnoreCase(serverMessage)) {
                    System.out.println("Server disconnected.");
                    break;
                } else if ("bye".equalsIgnoreCase(clientMessage)) {
                    System.out.println("Client disconnecting.");
                    break;
                }
            }

            socket.close();
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to the host.");
            System.exit(1);
        }
    }
}